package com.hit.comm.netty;

import com.hit.comm.dto.CommWrapperDTO;
import com.hit.comm.utils.ProtostuffUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author kehanjiang
 * @date 2025/03/24
 */
@Slf4j
public class MessageDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) {
        try {
            byte[] body = new byte[in.readableBytes()];  //传输正常
            in.readBytes(body);
            list.add(ProtostuffUtils.deserialize(body, CommWrapperDTO.class));
        } catch (Exception e) {
            log.error("decoder error : {}", e.getMessage());
        }
    }
}